#!/bin/bash
get_cookie(){
    cookie=$1
    dedeuserid=`echo $cookie|grep -o 'DedeUserID=[0-9]\{1,\}; DedeUserID__ckMd5=[a-z0-9_]\{1,\}'`
    sessdata=`echo $cookie|grep -o 'SESSDATA=[a-zA-Z0-9\"\%_]\{1,\}'`
    ckpv=`echo $cookie|grep -o 'ck_pv=[a-zA-Z0-9\_]\{1,\}'`
    ssid=`echo $cookie|grep -o 'SSID=[a-zA-Z0-9\_]\{1,\}; sid=[a-zA-Z0-9\_]\{1,\}'`
    livebuvid=`echo $cookie|grep -o 'LIVE_BUVID=[a-zA-Z0-9\_]\{1,\}; LIVE_BUVID__ckMd5=[a-zA-Z0-9\_]\{1,\}'`
    logindata=`echo $cookie|grep -o 'LIVE_LOGIN_DATA=[a-zA-Z0-9\_]\{1,\}; LIVE_LOGIN_DATA__ckMd5=[a-zA-Z0-9\_]\{1,\}'`
    cookie="${dedeuserid}; ${sessdata}; ${ckpv}; ${ssid}; ${livebuvid}; ${logindata}"
    cookie=`echo $cookie|sed 's#\"##g'`
}
##################
read -p "请输入Cookie：" cookie
clear
read -p "请输入直播房间ID（网址最后的数字）：" roomid
msg1="bilibili-(゜-゜)つロ乾杯~"
msg2="请大家注意弹幕礼仪哦！"
msg3="厉害了我的哥！"
msg4="向大佬低头"
msg5="那你很棒哦！"
echo -e "已内置5组回复语：\n$msg1\n$msg2\n$msg3\n$msg4\n$msg5"
read -p "如果想自行输入请输入（Y 或 N）：" answer
case $answer in
    [Yy])
    for((i=1;i<=5;i++)); do
        read -p "请输入第 $i 个回复语：" msg$i
    done
    ;;
    [Nn])
    echo "将使用内置回复语进行操作"
    ;;
esac
echo "######### 开 始 自 动 发 言 ##########"
get_cookie "$cookie"
while true; do
    for((i=1;i<=5;i++)); do
        eval msg=\$'msg'${i}
        msgs=$(php -r "echo rawurlencode('$msg');")
        result=`curl -s "http://live.bilibili.com/msg/send" -H "Cookie: $cookie" --data "color=16777215&fontsize=25&mode=1&msg=$msgs&roomid=$roomid"`
        if echo $result |grep -q ' 1s'; then
            echo "发言过快：$msg"
        elif echo $result |grep -q 'repeat'; then
            echo "发言重复：$msg"
        elif echo $result |grep -q 'code\":0,'; then
            echo "发言成功：$msg"
        else
            echo "发言失败：$msg"
        fi
        sleep 2
    done
done
